<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品编码" prop="coding">
      <el-input v-model="dataForm.coding" placeholder="商品编码"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="images">
      <el-input v-model="dataForm.images" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="商品介绍" prop="introduce">
      <el-input v-model="dataForm.introduce" placeholder="商品介绍"></el-input>
    </el-form-item>
    <el-form-item label="售后服务" prop="serve">
      <el-input v-model="dataForm.serve" placeholder="售后服务"></el-input>
    </el-form-item>
    <el-form-item label="商品分类ID 外键" prop="tid">
      <el-input v-model="dataForm.tid" placeholder="商品分类ID 外键"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          coding: '',
          name: '',
          images: '',
          price: '',
          introduce: '',
          commodityPrice: '',
          serve: '',
          tid: ''
        },
        dataRule: {
          coding: [
            { required: true, message: '商品编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          images: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          introduce: [
            { required: true, message: '商品介绍不能为空', trigger: 'blur' }
          ],
          commodityPrice: [
            { required: true, message: '商品总金额不能为空', trigger: 'blur' }
          ],
          serve: [
            { required: true, message: '售后服务不能为空', trigger: 'blur' }
          ],
          tid: [
            { required: true, message: '商品分类ID 外键不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/goods/commodity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.coding = data.commodity.coding
                this.dataForm.name = data.commodity.name
                this.dataForm.images = data.commodity.images
                this.dataForm.price = data.commodity.price
                this.dataForm.introduce = data.commodity.introduce
                this.dataForm.commodityPrice = data.commodity.commodityPrice
                this.dataForm.serve = data.commodity.serve
                this.dataForm.tid = data.commodity.tid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/goods/commodity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'coding': this.dataForm.coding,
                'name': this.dataForm.name,
                'images': this.dataForm.images,
                'price': this.dataForm.price,
                'introduce': this.dataForm.introduce,
                'commodityPrice': this.dataForm.commodityPrice,
                'serve': this.dataForm.serve,
                'tid': this.dataForm.tid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
